<include file="Common/header" />
<!-- 顶部 -->     
<div id="middle">
    <include file="Common/menu" />
    <div class="right" id="mainFrame">
		<div class="right_cont">
			<ul class="breadcrumb">当前位置：
				<a href="javascript:void(0);">首页</a> <span class="divider">/</span>
				<a href="javascript:void(0);">财务管理</a> <span class="divider">/</span>
				房租收缴
			</ul>
			<div class="title_right"><strong>按条件查询</strong></div>
			<form action="" method="POST" id="addfrom">
				<div>
					<table class="table table-bordered" style="width: 80%;" align="center">
						<tr>
							<td width="20%" align="right" bgcolor="#f1f1f1">房间号：</td>
							<td width="30%">
								<input type="text" name="room_number" id="room_number" class="span1-3" value="" />
								<span class="usernameabel"></span>
							</td>
							<td align="left">
								<input type="button" value="查询" class="btn btn-info querys" style="width:80px;" />
							</td>
						</tr>
					</table>
				</div>
			</form>
			<div class="title_right"><strong>会员信息</strong></div>
			<div style="width: 900px; margin:auto">
				<table class="table table-bordered">
					<tr>
						<td width="20%" align="right" bgcolor="#f1f1f1">姓名：</td>
						<td width="30%">
							<input type="text" name="field1" id="field1" class="span1-3" value="" />
						</td>
						<td width="20%" align="right" bgcolor="#f1f1f1">合同号：</td>
						<td width="30%">
							<input type="text" name="field2" id="field2" class="span1-3" value="" />
						</td>
					</tr>
					<tr>
						<td width="20%" align="right" bgcolor="#f1f1f1">房租开始时间：</td>
						<td width="30%">
							<input type="text" name="field3" id="field3" class="span1-3" value="" />
						</td>
						<td width="20%" align="right" bgcolor="#f1f1f1">房租结束时间：</td>
						<td width="30%">
							<input type="text" name="field4" id="field4" class="span1-3" value="" />
						</td>
					</tr>
				</table>
			</div>
			<div style="width: 100%; overflow:hidden;">
				<div style="width: 55%; float: left;">
					<div class="title_right"><strong>费用详情</strong></div>
					<table class="table table-bordered"> 
						<tr>
							<td align="right" bgcolor="#f1f1f1">押金：</td>
							<td>
								<input type="text" name="deposit" id="deposit" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="deposit" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
							<td align="right" bgcolor="#f1f1f1">房租：</td>
							<td>
								<input type="text" name="rent" id="rent" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="rent" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1 rentss" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
						</tr>
						<tr>	
							<td align="right" bgcolor="#f1f1f1">物业费：</td>
							<td>
								<input type="text" name="property" id="property" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="property" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
							<td align="right" bgcolor="#f1f1f1">定金：</td>
							<td>
								<input type="text" name="deposit_first" id="deposit_first" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="deposit_first" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">水电气：</td>
							<td>
								<input type="text" name="water_power" id="water_power" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="water_power" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
							<td align="right" bgcolor="#f1f1f1">其他：</td>
							<td>
								<input type="text" name="other" id="other" class="span1-3 keyups" value="0" />
								<span class="usernameabel">元</span>
								<div class="div_right">
									<input type="checkbox" attkey="other" name="parentCheckBox" id="parentCheckBox" class="nodeCheckbox nodeCheckbox_1" parent_id="1" style="vertical-align:middle; float: left;" value="1" checked />&nbsp;&nbsp;
								</div>
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">开始时间：</td>
							<td>
								<input type="text" name="begin_time" id="begin_time" class="laydate-icon span1-3" value="" />
							</td>
							<td align="right" bgcolor="#f1f1f1">终止时间：</td>
							<td>
								<input type="text" name="end_time" id="end_time" class="laydate-icon span1-3" value="" />
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">缴费月数：</td>
							<td>
								<select id="monthc" name="monthc">
									<option value="1">1</option>
									<option value="2">2</option>
									<option value="3">3</option>
									<option value="4">4</option>
									<option value="5">5</option>
									<option value="6">6</option>
									<option value="7">7</option>
									<option value="8">8</option>
									<option value="9">9</option>
									<option value="10">10</option>
									<option value="11">11</option>
									<option value="12">12</option>
								</select>
							</td>
							<td align="right" bgcolor="#f1f1f1">合计：</td>
							<td>
								<input type="text" name="acount" id="acount" class="span1-3" value="0" />
								<span class="usernameabel"></span>
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">备注：</td>
							<td>
								<input type="text" name="remark" id="remark" class="span1-3" value="" />
								<span class="usernameabel" style="color: red;"> * 请备注收费说明</span>
							</td>
							<td>收费时间：</td>
							<td>
								<input type="text" name="paytime" id="paytime" class="laydate-icon span1-3" value="" />
							</td>
						</tr>
					</table>
				</div>
				<div style="width: 40%; float: left; margin-left: 10px;">
					<div class="title_right"><strong>支付方式</strong></div>
					<table class="table table-bordered">
						<tr>
							<td align="right" bgcolor="#f1f1f1">现金：</td>
							<td>
								<input type="text" name="xianjin" id="xianjin" class="span1-3" value="0" />
								<span class="usernameabel">元</span>
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">拉卡拉：</td>
							<td>
								<input type="text" name="lakala" id="lakala" class="span1-3" value="0" />
								<span class="usernameabel">元</span>
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">支付宝：</td>
							<td>
								<input type="text" name="alipay" id="alipay" class="span1-2" value="0" />
								<span class="usernameabel">元</span>
								
								<span style="padding-left: 20px;">支付订单号：</span><input type="text" name="alipay_ordersn" id="alipay_ordersn" class="span1-3" value="" />
							</td>
						</tr>
						<tr>
							<td align="right" bgcolor="#f1f1f1">微信：</td>
							<td>
								<input type="text" name="weixin" id="weixin" class="span1-2" value="0" />
								<span class="usernameabel">元</span>
								
								<span style="padding-left: 20px;">支付订单号：</span><input type="text" name="weixin_ordersn" id="weixin_ordersn" class="span1-3" value="" />
							</td>
						</tr>
					</table>
				</div>
			</div>	
			<table  class="margin-bottom-20 table  no-border" >
				<tr>
					<td class="text-center">
						<input type="button" value="生成二维码" class="btn btn-danger roleSubmit" style="width:80px;" />
						<input type="button" value="确认保存" class="btn btn-success paysuccess" style="width:80px;" />
					</td>
				</tr>
			</table>
			<div style="width: 80%; overflow:hidden; text-align: center; padding-left: 100px;">
				<div style="width: 40%; float: left; margin-left: 10px; display: none;" class="weixin">
					<div class="title_right"><strong>微信</strong></div>
					<img src="" class="weixin" />
				</div>
				
				<div style="width: 40%; float: left; margin-left: 10px; display: none;" class="alipay">
					<div class="title_right"><strong>支付宝</strong></div>
					<img src="" class="alipay" />
				</div>
			</div>
			
			<div class="title_right"><strong>费用收缴记录</strong></div>
			<div id="ajaxDiv" style="height:auto; overflow:scroll;">
				<include file="Paylog/create_list_2" />
			</div>
		</div>     
    </div>
</div>
<script>
	function checkNumber(number){
		var digit = /^-?\d+(\.\d+)?$/;
        if (!digit.test(number)){
            alert("只能输入负数或数字");
            inputObj.focus();
        }
	}

	$(document).ready(function(){
		var fromData = {};
		$(".roleSubmit").on('click', function(){
			if(fromData.contract){
				var load = layer.load(2);
				
				$(".keyups").each(function(){
					if($(this).closest('td').find('.nodeCheckbox').is(':checked')){
						fromData[$(this).attr('name')] = parseFloat($(this).val());
					}else{
						fromData[$(this).attr('name')] = 0;
					}
				});
				
				var remark = $("#remark").val() || "";
				if(remark == "" || remark.length <= 0){
					layer.close(load);
					layer.msg("备注不能为空", {icon: 5, time: 2000});
					return false;
				}
				fromData['remark'] = remark;
				fromData['pay_type'] = $("#pay_type").val();
				fromData['pay_card'] = $("#pay_card").val();
				//增加收费时间
				fromData['paytime'] = $("#paytime").val();
				//判断是否收取房租
				if($(".rentss").is(':checked')){
					//判断时间
					var begin_time = $("#begin_time").val();
					var end_time = $("#end_time").val();

					if(begin_time.length <= 0){
						layer.close(load);
						layer.msg("请选择房租开始时间", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['begin_time'] = begin_time;
					}
					if(end_time.length <= 0){
						layer.close(load);
						layer.msg("请选择房租结束时间", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['end_time'] = end_time;
					}
				}

				var weixinTotal = $("#weixin").val(),
					alipayTotal = $("#alipay").val(),
					xianjinTotal = $("#xianjin").val(),
					lakalaTotal = $("#lakala").val();
				//判断分配金额是否为数字
				if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal)){
					//判断金额是否一致
					var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1;
					if(total != $("#acount").val()/1){
						layer.close(load);
						layer.msg('支付金额分配错误，请重新分配。');
						return;
					}

					fromData['xianjin'] = xianjinTotal;
					fromData['lakala'] = lakalaTotal;
					fromData['alipay'] = alipayTotal;
					fromData['weixin'] = weixinTotal;
				}else{
					layer.close(load);
					layer.msg('请正确分配支付金额比例.');
					return false;
				}
				
				$.post('ajaxcreate.html', {json: JSON.stringify(fromData)}, function(data){
					layer.close(load);
					if(data.errCode/1){
						layer.msg('操作成功', {icon: 1, time: 2000}, function(){
							if(data.weixin == '' && data.alipay == ''){
								location.reload();
							}else{
								if($("#weixin").val()/1){
									$("div.weixin").show();
									$("img.weixin").attr('src', "__PUBLIC__/ercode/"+ data.weixin);
								}else{
									$("div.weixin").hide();
								}
								
								if($("#alipay").val()/1){
									$("div.alipay").show();
									$("img.alipay").attr('src', "__PUBLIC__/ercode/"+ data.alipay);
								}else{
									$("div.alipay").hide();
								}
							}
							//开始查询状态
							ajax_function(data.gobackid, fromData.roomnumber, fromData.userid);
						});
					}else{
						layer.msg('操作失败', {icon: 2, time: 2000});
					}
				});
			}else{
				layer.close(load);
				layer.msg('请先查询目标房间数据');
			}
		});
		
		//确定保存
		$(".paysuccess").on('click', function(){
			if(fromData.contract){
				var load = layer.load(2);
				
				$(".keyups").each(function(){
					if($(this).closest('td').find('.nodeCheckbox').is(':checked')){
						fromData[$(this).attr('name')] = parseFloat($(this).val());
					}else{
						fromData[$(this).attr('name')] = 0;
					}
				});
				
				var remark = $("#remark").val() || "";
				if(remark == "" || remark.length <= 0){
					layer.close(load);
					layer.msg("备注不能为空", {icon: 5, time: 2000});
					return false;
				}
				fromData['remark'] = remark;

				fromData['pay_type'] = $("#pay_type").val();
				fromData['pay_card'] = $("#pay_card").val();
				//增加收费时间
				fromData['paytime'] = $("#paytime").val();
				//判断是否收取房租
				if($(".rentss").is(':checked')){
					//判断时间
					var begin_time = $("#begin_time").val();
					var end_time = $("#end_time").val();

					if(begin_time.length <= 0){
						layer.close(load);
						layer.msg("请选择房租开始时间", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['begin_time'] = begin_time;
					}
					if(end_time.length <= 0){
						layer.close(load);
						layer.msg("请选择房租结束时间", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['end_time'] = end_time;
					}
				}

				var weixinTotal = $("#weixin").val(),
					alipayTotal = $("#alipay").val(),
					xianjinTotal = $("#xianjin").val(),
					lakalaTotal = $("#lakala").val();
				//判断分配金额是否为数字
				if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal)){
					//判断金额是否一致
					var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1;
					if(total != $("#acount").val()/1){
						layer.close(load);
						layer.msg('支付金额分配错误，请重新分配。');
						return;
					}

					fromData['xianjin'] = xianjinTotal;
					fromData['lakala'] = lakalaTotal;
					fromData['alipay'] = alipayTotal;
					fromData['weixin'] = weixinTotal;
					fromData['alipay_ordersn'] = $("#alipay_ordersn").val();
					fromData['weixin_ordersn'] = $("#weixin_ordersn").val();
				}else{
					layer.close(load);
					layer.msg('请正确分配支付金额比例.');
					return false;
				}
				
				$.post('ajaxcreate2.html', {json: JSON.stringify(fromData)}, function(data){
					layer.close(load);
					if(data.errCode/1){
						layer.msg('操作成功',{icon: 1, time: 2000}, function(){
							//刷新网页
							location.reload();
						});
					}else{
						layer.msg('操作失败',{icon: 1, time: 2000});
					}
				});
			}else{
				layer.close(load);
				layer.msg('请先查询目标房间数据');
			}
		});
		
		$(".querys").on('click', function(){
			var where = {};
				where.btime = ""; //$("#btime").val();
				where.etime = "";//$("#etime").val();
				where.roomnumber = $("#room_number").val();
			if($("#room_number").val() == ''){
				layer.msg('请输入房间号');
				return;
			}
			
			/*
			if($("#btime").val() == "" || $("#etime").val() == ""){
				layer.msg('请选择日期');
				return;
			}*/
			
			var load = layer.load(2);
			$.get('ajaxcreate.html', where, function(data){
				layer.close(load);
				if(data.errCode/1){
					fromData = data.info;
					//Set Value
					$("#deposit").val(fromData.deposit);
					$("#deposit_first").val(fromData.deposit_first);
					$("#water_power").val(fromData.water_power);
					$("#property").val(fromData.property);
					$("#rent").val(fromData.rent);
					$("#card_peposit").val(fromData.card_peposit);
					$("#acount").val(fromData.acount);
					$("#other").val(fromData.other);
					//加载会员详情
					var uinfo = data.field;
					$("#field2").val(uinfo.contract);
					$("#field1").val(uinfo.nickname);
					$("#field3").val(uinfo.begintime);
					$("#field4").val(uinfo.endtime);
					//加载日志
					$("#ajaxDiv").html(data.html);
				}else{
					layer.msg('未查询到相关信息');
				}
			});
		});
		
		$(".nodeCheckbox").on('change', function(){
			var obj = $(this);
			var attkey = obj.attr('attkey');
			var acount = $("#acount").val()/1;
			var ncount = $("#"+ attkey).val()/1;
			
			if(obj.is(':checked')){
				acount += ncount;
			}else{
				acount -= ncount;
			}
			
			$("#acount").val(acount.toFixed(2));
		});
		
		$(".keyups").on('keyup', function(event){
			var vals = $(this).val() || 0;
			//1. 判断是否为数字
			if(!isNaN(vals)){
				var acount = 0;
				$(".keyups").each(function(){
					if($(this).closest('td').find('.nodeCheckbox').is(':checked')){
						acount += parseFloat($(this).val());
					}
				});

				$("#acount").val(acount.toFixed(2));
			}
		});

		var amonth=1;
		//月数选择
		$("#monthc").on('change', function(){
			if(fromData.rent){
				var month = $(this).val()/1 || 1;
				if(month > 1){
					
				}else{
					month = 1;
				}
				var mcount = fromData.rent*month;
				$("#rent").val(mcount.toFixed(2));
				//acount
				//判断是减还是加
				if(month > amonth){
					mcount = (month - amonth) * fromData.rent;
					var acount = $("#acount").val()/1 + mcount;
				}else{
					mcount = (amonth - month) * fromData.rent;
					var acount = $("#acount").val()/1 - mcount;
				}
				$("#acount").val(acount.toFixed(2));
				amonth = month;
			}else{
				layer.msg('请先查询房间信息');
			}
		});

	});
	
	var ajax_function = function(gobackid, roomnumber, uuid){
		$.get('ajaxcreateresult.html',{gobackid: gobackid, roomnumber: roomnumber, uuid: uuid}, function(data){
			if(data.errCode/1){
				$("#ajaxDiv").html(data.html);
			}
			
			setTimeout(function(){
				ajax_function(gobackid, roomnumber, uuid);
			}, 10000);
		});
	}
</script>
<script>
!function(){
	laydate.skin('molv');
	laydate({elem: '#begin_time', format: "YYYY-MM-DD hh:mm:ss"});
	laydate({elem: '#end_time', format: "YYYY-MM-DD hh:mm:ss"});
	laydate({elem: '#paytime', format: "YYYY-MM-DD hh:mm:ss"});
}();
</script>
<include file="Common/footer" />
